//! Сцена
class Scene3D : public Scene {
protected:
	
	//! Рисуем сцену
	void _on_draw(){
		// обновляем положение камеры
		camera.bind();
		// рисуем небо
		glColor4f(1,1,1,1);
		skybox._draw(camera.skybox_matrix);
		// настрока вывода
		glEnable(GL_TEXTURE_2D);
		glEnable(GL_CULL_FACE);
		glMatrixMode(GL_PROJECTION);
		glLoadMatrixf(value_ptr(camera.projection_matrix));
		// рисуем слои сцены
		for (ModelMap::iterator it=model.begin(); it!=model.end(); ++it)
			(*it).second._draw();
		on_draw();
	}
	
	//! Обработка событий
	virtual void on_message(const Message& msg){
		switch (msg.type){
		case GE_MESSAGE_REDRAW:
			_on_draw();
			break;
		case GE_MESSAGE_ON_FRAME:
			on_frame();
			break;
		case GE_MESSAGE_RESIZE:
			on_resize(event.front().value.as_int[0],event.front().value.as_int[1]);
			break;
		case GE_MESSAGE_INIT:
			on_init();
			break;
		case GE_MESSAGE_PAUSE:
			on_pause();
			break;
		case GE_MESSAGE_CONTINUE:
			on_continue();
			break;
		case GE_MESSAGE_CLOSE:
			on_close();
			break;
		case GE_MESSAGE_CHAR:
			on_char(event.front().value.as_int[0],event.front().value.as_int[1]);
			break;
		case GE_MESSAGE_KEY:
			on_key(event.front().value.as_int[0],event.front().value.as_int[1]);
			break;
		case GE_MESSAGE_MOUSE_MOVE:
			if (camera.mouse_control&&System::device().mouse.button[GE_MOUSE_RIGHT_BUTTON]){
				camera.direction+=System::device().mouse.xrel*camera.direction_speed;
				camera.angle+=System::System::device().mouse.yrel*camera.angle_speed;
			} else
				on_mouse_move(event.front().value.as_int[0],event.front().value.as_int[1]);
			break;
		case GE_MESSAGE_MOUSE_BUTTON:
			on_mouse_button(event.front().value.as_int[0],event.front().value.as_int[1]);
			break;
		case GE_MESSAGE_MOUSE_WHEEL:
			if (camera.mouse_control){
				camera.distance+=event.front().value.as_int[0]?camera.distance_speed:-camera.distance_speed;
			} else
				on_mouse_wheel(event.front().value.as_int[0]);
			break;
		}
	}
	
public:

	//! Камера
	Camera camera;

	//! Скайбокс
	Skybox skybox;
	
	//! Тип карты моделей
	typedef map <string,Model> ModelMap;

	//! Карта моделей
	ModelMap model;	
	
	//! События пользователя
	virtual void on_init(){}
	virtual void on_close(){}
	virtual void on_pause(){}
	virtual void on_continue(){}
	virtual void on_frame(){}
	virtual void on_resize(int width, int height){glViewport(0,0,System::device().screen.width,System::device().screen.height);}
	virtual void on_mouse_move(int x, int y){}
	virtual void on_mouse_button(int button, bool action){}
	virtual void on_mouse_wheel(bool action){}
	virtual void on_key(int key,bool action){}
	virtual void on_char(wchar_t ch, bool action){}
	virtual void on_draw(){}
	
public:

	typedef map<string,Material> MaterialMap;
	
	MaterialMap material;
	
};